﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using TDT.Land.BusinessLogic;
using TDT.Land.Models;
using TDT.Land.Utilities;
using TDT.Land.Entity;
using System.Text.RegularExpressions;

namespace TDT.Land.Web.Controllers
{
    public class HomeController : BaseController
    {
        //
        // GET: /Home/



        public ActionResult Home()
        {

            return View();
        }

        public ActionResult Index()
        {
            Session["menuSelect"] = "m1";
           // ViewBag.docBody = "nguyenduy";
           //// var ext = AdminRepo.Instance.GetProBuildingByCode("home-info");
           // if (ext !=null)
           // ViewBag.Bcontent = ext.Description;

            //Get Bất động San

            List<Pro_LandModel> lstLand = AdminRepo.Instance.GetProLands().OrderByDescending(d=>d.CreateDate).Take(10).ToList();
            foreach (Pro_LandModel itemLand in lstLand)
            {
                if (!string.IsNullOrEmpty(itemLand.Description))
                {
                    itemLand.Description = Regex.Replace(itemLand.Description, @"<img\s[^>]*>(?:\s*?/>)?", "", RegexOptions.IgnoreCase);
                    itemLand.Description = Regex.Replace(itemLand.Description, @"<(.|\n)*?>", string.Empty);
                    int descLength = itemLand.Description.Length;
                    if (descLength > 250)
                        itemLand.Description = itemLand.Description.Substring(0, 250) + "...";
                }
            }
            ViewBag.lstLand = lstLand;


            //get news for home
            string buidingType = CategoryType.W_NEW_TYPE.ToString();
            List<Pro_Building> lstBuidingTop = AdminRepo.Instance.GetProBuildingByType(buidingType).Where(m=> m.IsSlideShow == true ).OrderByDescending(m => m.DateCreate).Take(6).ToList();           
            foreach (var itemData in lstBuidingTop)
            {
                 itemData.Description  = Regex.Replace(itemData.Description , @"<img\s[^>]*>(?:\s*?/>)?", "", RegexOptions.IgnoreCase);
                itemData.Description  = Regex.Replace(itemData.Description , @"<(.|\n)*?>", string.Empty);
                int descLength = itemData.Description.Length;
                if (descLength > 250)
                    itemData.Description = itemData.Description.Substring(0,250) + "...";
            }
            ViewBag.lstNews = lstBuidingTop;

            //check show event

            bool checkEvent = AdminRepo.Instance.checkShowEvent();
            ViewBag.ShowEvent = checkEvent;
            
            return View();
        }

        public ActionResult Login(string userName, string password)
        {
            
            return View("Home");
        }

        public ActionResult testpopup()
        {
            return View();
        }


        // set session for menu
        public ActionResult SetSessionSelect(string menuId)
        {
            Session["menuSelect"] = menuId;
            //Session.Remove("menuLeft");
            return null;
        }


        public ActionResult GetSearch()
        {
           
            List<Cat_LandModel> lstHomeType = AdminRepo.Instance.GetCatLands(CategoryType.E_LAND_HOME_TYPE.ToString());
            List<Cat_LandModel> lstBDSType = AdminRepo.Instance.GetCatLands(CategoryType.E_LAND_BDS_TYPE.ToString());

            ViewBag.sel_Search_BDS = Commons.ConvertToSelectListItems(lstBDSType, Cat_LandModel.FieldName.Id, Cat_LandModel.FieldName.LandCategoryName);
            ViewBag.sel_Search_Type = Commons.ConvertToSelectListItems(lstHomeType, Cat_LandModel.FieldName.Id, Cat_LandModel.FieldName.LandCategoryName);

            List<Cat_ProvinceModel> lstProvince = AdminRepo.Instance.GetProvinces();
            ViewBag.sel_Search_Province = Commons.ConvertToSelectListItems(lstProvince, Cat_ProvinceModel.FieldName.Id, Cat_ProvinceModel.FieldName.ProvinceName);

            return PartialView("US_Search_left");
        }


        public ActionResult getDistrict(Guid ProId)
        {
           List<Cat_DistrictModel> lstWard = AdminRepo.Instance.GetDistricts(ProId);           
            ViewBag.select_DisTrict = Commons.ConvertToSelectListItems(lstWard, Cat_DistrictModel.FieldName.Id, Cat_DistrictModel.FieldName.DistrictName);
            return PartialView("US_District_view");
        }

        public ActionResult getWard(Guid disId)
        {
            List<Cat_WardModel> lstWard = AdminRepo.Instance.GetWards(disId);
            ViewBag.select_Ward = Commons.ConvertToSelectListItems(lstWard, Cat_WardModel.FieldName.Id, Cat_WardModel.FieldName.WardName);
            return PartialView("US_Ward_view");
        }



    }
}
